package com.gsk.europepharma.salesReport.utils;

import java.io.IOException;
import java.util.GregorianCalendar;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletResponse;

public class CacheFilter implements Filter {

	private FilterConfig config = null;
	
	/* (non-Javadoc)
	 * @see javax.servlet.Filter#destroy()
	 */
	public void destroy() {
		// TODO Auto-generated method stub
		
	}

	/* (non-Javadoc)
	 * @see javax.servlet.Filter#doFilter(javax.servlet.ServletRequest, javax.servlet.ServletResponse, javax.servlet.FilterChain)
	 */
	public void doFilter(ServletRequest request, ServletResponse response,
			FilterChain chain) throws IOException, ServletException {
		HttpServletResponse resp = (HttpServletResponse) response;
		long delayMilis = Integer.parseInt(config.getInitParameter("delayMinutes")) * 60*1000;
		long expDateMili = GregorianCalendar.getInstance().getTimeInMillis() + delayMilis;
		resp.setDateHeader("Expires", expDateMili);
		// accepter cache client
		resp.setHeader("Pragma", "");
		resp.setHeader("Cache-control", "private");
		
		/*Calendar d = GregorianCalendar.getInstance();
		d.setTimeInMillis(expDateMili);
		DateFormat df = SimpleDateFormat.getInstance();
		System.out.println("setting expires of " + request.getLocalAddr() + " to " + df.format(d.getTime()));*/
		chain.doFilter(request, response);
	}

	/* (non-Javadoc)
	 * @see javax.servlet.Filter#init(javax.servlet.FilterConfig)
	 */
	public void init(FilterConfig filterConfig) throws ServletException {
		this.config = filterConfig;
		
	}
	

}
